import 'dart:math';

class MathUtil {

   static String byteToHexString(int data){
    String hex = (data & 0xFF).toRadixString(16);
    if (hex.length == 1){
      hex = "0" + hex;
    }
    return  hex;
  }

  /**
   * 16进制转2进制
   * @param a
   * @return
   */
   static String HToB(String a) {
    String b = int.parse(toD(a, 16)).toRadixString(2);
    if (b.length == 7){
      b = "0" + b;
    }else  if (b.length == 6){
      b = "00" + b;
    }else  if (b.length == 5){
      b = "000" + b;
    }else  if (b.length == 4){
      b = "0000" + b;
    }else  if (b.length == 3){
      b = "00000" + b;
    }else  if (b.length == 2){
      b = "000000" + b;
    }else  if (b.length == 1){
      b = "0000000" + b;
    }else  if (b.isEmpty){
      b = "00000000";
    }
    return b;
  }

  static String toD(String a, int b) {
    int r = 0;
    for (int i = 0; i < a.length; i++) {
      r =  (r + formatting(a.substring(i, i + 1))
          * pow(b, a.length - i - 1)).toInt();
    }
    return r.toString();
  }

  // 将十六进制中的字母转为对应的数字
   static int formatting(String a) {
    int i = 0;
    for (int u = 0; u < 10; u++) {
      if (a==u.toString()) {
        i = u;
      }
    }
    if (a=="a") {
      i = 10;
    }
    if (a=="b") {
      i = 11;
    }
    if (a=="c") {
      i = 12;
    }
    if (a=="d") {
      i = 13;
    }
    if (a=="e") {
      i = 14;
    }
    if (a=="f") {
      i = 15;
    }
    return i;
  }

}
